var e, t = require("@vant/area-data"), a = (e = require("@vant/weapp/toast/toast")) && e.__esModule ? e : {
    default: e
};

var i = require("../../utils/request"), d = require("../../utils/api"), s = require("../../utils/date"), o = require("../../utils/storage"), r = (require("../../utils/config"), 
require("../../utils/encrypt")), c = require("../../utils/check");

Page({
    data: {
        themeColor: getApp().globalData.themeColor,
        loading: !1,
        areaList: t.areaList,
        showSelectBirthdayPopup: !1,
        showSelectAddressPopup: !1,
        maxDate: new Date().getTime(),
        minDate: new Date(1900, 1, 1).getTime(),
        currentDate: new Date(1990, 0, 1).getTime(),
        optionSex: [ {
            text: "男",
            value: 0
        }, {
            text: "女",
            value: 1
        } ],
        userName: "",
        canEditUserName: !1,
        phoneNumber: "",
        saveMobile: "",
        showMobile: "",
        sex: 0,
        relationship: "",
        nameShow: "",
        nameValue: "",
        nameIcon: "eye-o",
        nameDisabled: !1,
        certificateType: "",
        certificateNoShow: "",
        certificateNoValue: "",
        certificateNoIcon: "eye-o",
        certificateNoDisabled: !1,
        birthday: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        addressStrShow: "",
        address: ""
    },
    onLoad: function() {
        var e = this, t = o.getStorageSync(o.storageKey.phoneNumber);
        i.request(i.HttpMethod.GET, d.apiRoute.selectFamilyByMyself, {}).then(function(i) {
            if ("20000" == i.code) {
                var d = "";
                "" != i.data.provinceCode && null != i.data.provinceCode && (d += e.data.areaList.province_list[i.data.provinceCode] + " "), 
                "" != i.data.cityCode && null != i.data.cityCode && (d += e.data.areaList.city_list[i.data.cityCode] + " "), 
                "" != i.data.districtCode && null != i.data.districtCode && (d += e.data.areaList.county_list[i.data.districtCode]), 
                null == i.data.birthday && (i.data.birthday = ""), "" != i.data.birthday && e.setData({
                    currentDate: Number(i.data.birthday)
                }), e.setData({
                    sex: i.data.gender,
                    mobile: i.data.mobile,
                    nameShow: i.data.name,
                    nameValue: i.data.name,
                    gender: i.data.gender,
                    mobileShow: i.data.mobile,
                    mobileValue: i.data.mobile,
                    certificateType: i.data.certificateType,
                    certificateNoShow: i.data.certificateNo,
                    certificateNoValue: i.data.certificateNo,
                    birthday: "" == i.data.birthday ? "" : s.formatDate(new Date(Number(i.data.birthday)), "yyyy-MM-dd"),
                    provinceCode: i.data.provinceCode,
                    cityCode: i.data.cityCode,
                    districtCode: i.data.districtCode,
                    addressStrShow: d,
                    address: i.data.address
                });
                var o = "", c = "";
                null != t && "" != t && (o = t, c = r.encryptMobile(t)), e.setData({
                    saveMobile: o,
                    showMobile: c
                });
            } else e.setData({
                loading: !1
            }), (0, a.default)({
                message: i.message,
                duration: 5e3
            });
        });
    },
    relationshipChange: function(e) {
        this.setData({
            relationship: e.detail
        });
    },
    genderChange: function(e) {
        this.setData({
            gender: e.detail
        });
    },
    certificateTypeChange: function(e) {
        this.setData({
            certificateType: e.detail
        });
    },
    nameIconClick: function() {
        this.setData({
            nameDisabled: !this.data.nameDisabled,
            nameIcon: "closed-eye" == this.data.nameIcon ? "eye-o" : "closed-eye",
            nameShow: r.encryptName(this.data.nameValue)
        });
    },
    certificateNoIconClick: function() {
        this.setData({
            certificateNoDisabled: !this.data.certificateNoDisabled,
            certificateNoIcon: "closed-eye" == this.data.certificateNoIcon ? "eye-o" : "closed-eye",
            certificateNoShow: r.encryptIdCard(this.data.certificateNoValue)
        });
    },
    certificateNoChange: function() {
        if ("0" == this.data.certificateType) {
            var e = c.getBirthDayFromIdCard(this.data.certificateNoValue), t = new Date(e).getTime();
            if (isNaN(t)) this.setData({
                birthday: "",
                currentDate: new Date(1990, 0, 1).getTime()
            }); else {
                var a = s.formatDate(new Date(Number(t)), "yyyy-MM-dd");
                this.setData({
                    birthday: a,
                    currentDate: t
                });
            }
        }
    },
    onClickSelectBirhtday: function() {
        this.setData({
            showSelectBirthdayPopup: !0
        });
    },
    onCloseSelectBirthday: function() {
        this.setData({
            showSelectBirthdayPopup: !1
        });
    },
    confirmSelectBirthday: function(e) {
        this.setData({
            showSelectBirthdayPopup: !1,
            birthday: s.formatDate(e.detail, "yyyy-MM-dd")
        });
    },
    onClickSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !0
        });
    },
    onCloseSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !1
        });
    },
    confirmSelectAddress: function(e) {
        var t = "", i = e.detail.values, d = !0;
        i.forEach(function(e) {
            null != e && null != e.code && null != e.name && "" != e.code && "" != e.name ? t += e.name + " " : d = !1;
        }), d ? (this.setData({
            provinceCode: i[0].code,
            cityCode: i[1].code,
            districtCode: i[2].code
        }), this.setData({
            showSelectAddressPopup: !1,
            addressStrShow: t
        })) : (0, a.default)({
            message: "请选择完整的省市区",
            duration: 5e3
        });
    },
    updateUserBase: function() {
        var e = this;
        "" != this.data.nameValue ? 0 == this.data.sex || 1 == this.data.sex ? "" != this.data.certificateType ? "" != this.data.certificateNoValue ? "0" != this.data.certificateType || c.isIdCard(this.data.certificateNoValue) ? "" != this.data.birthday ? (this.setData({
            loading: !0
        }), i.request(i.HttpMethod.POST, d.apiRoute.updateUserBase, {
            userName: this.data.userName,
            mobile: this.data.saveMobile,
            sex: this.data.sex,
            name: this.data.nameValue,
            certificateType: this.data.certificateType,
            certificateNo: this.data.certificateNoValue,
            birthday: this.data.birthday,
            provinceCode: this.data.provinceCode,
            cityCode: this.data.cityCode,
            districtCode: this.data.districtCode,
            address: this.data.address
        }).then(function(t) {
            if ("20000" == t.code) {
                var i = o.getStorageSync(o.storageKey.userBase);
                i.mobile = e.data.phoneNumber, i.sex = e.data.sex, i.userName = e.data.userName, 
                i.name = e.data.nameValue, i.certificateNo = e.data.certificateNoValue, i.certificateType = e.data.certificateType, 
                o.setStorageSync(o.storageKey.userBase, i), (0, a.default)({
                    message: "保存成功",
                    duration: 5e3
                }), e.setData({
                    loading: !1
                }), wx.navigateBack({
                    delta: 1
                });
            } else e.setData({
                loading: !1
            }), (0, a.default)({
                message: t.message,
                duration: 5e3
            });
        })) : (0, a.default)({
            message: "请输入出生日期",
            duration: 5e3
        }) : (0, a.default)({
            message: "身份证号格式不正确",
            duration: 5e3
        }) : (0, a.default)({
            message: "请输入证件号码",
            duration: 5e3
        }) : (0, a.default)({
            message: "请选择证件类型",
            duration: 5e3
        }) : (0, a.default)({
            message: "请选择性别",
            duration: 5e3
        }) : (0, a.default)({
            message: "请输入姓名",
            duration: 5e3
        });
    }
});